/*
 * TimeUnit.java
 *
 * Created on 4. September 2007, 16:20
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package timeitj.persistence;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import org.apache.log4j.Logger;

/**
 *
 * @author kunee
 */
@Entity
@Table(name="TIMEUNIT")
public class TimeUnit implements Serializable {
    
    private static Logger logger = Logger.getLogger(TimeUnit.class.getName());
    
    @Id @GeneratedValue
    @Column(name="ID")
    private long id;
    
    @Version
    @Column(name="OPT_LOCK")
    private Long version;
    
    @Column(name="STARTDATE")
    @Temporal(value = TemporalType.DATE)
    private Date startDate;
    
    @Column(name="ENDDATE")
    @Temporal(value = TemporalType.DATE)
    private Date endDate;
    
    @Column(name="TIMELENGTH")
    private long timeLength;
    
    @ManyToOne
    private Participance participance;
    
    /** Creates a new instance of TimeUnit */
    public TimeUnit() {
    }
    
    public long getId() {
        return id;
    }
    
    public Date getStartDate() {
        return startDate;
    }
    
    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }
    
    public Date getEndDate() {
        return endDate;
    }
    
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }
    
    public long getTimeLength() {
        //TODO: ist das okay?
        if (timeLength == 0) {
            timeLength = getEndDate().getTime() - getStartDate().getTime();
        }
        return timeLength;
    }
    
    public void setTimeLength(long timeLength) {
        this.timeLength = timeLength;
    }
    
    public Participance getParticipance() {
        return participance;
    }
    
    public void setParticipance(Participance participance) {
        this.participance = participance;
    }
    
}
